package homework6;
import java.io.*;
import java.util.*;
public class price {
	public static void main(String args[]) {
		File file = new File("Price.txt");
		Scanner sc = null;
		int count = 0;
		double sum = 0;
		try {double alprice = 0;
		     sc = new Scanner(file);
		     sc.useDelimiter("[^0123456789.]+");
		     while(sc.hasNextDouble()) {
			      alprice = sc.nextDouble();
			      count++;
			      sum = sum + alprice;
			
			
			      System.out.println(alprice);
		     }
		     double aver = sum/count;
		     String str = String.format("%.3f",aver);
		     System.out.println("平均价格:"+str);
		     String filePath = "Price.txt";
	            BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true));
	            writer.newLine();
	            writer.write("平均价格：");
	            writer.newLine(); // 插入换行符
	            writer.write(str + "元");
	            writer.close();
	            System.out.println("文件创建成功并且内容已写入。"); 
	   }
		
		catch(Exception exp) {
			System.out.println(exp);
		}
		
	}
	
}
